const serverDao = require('../dao/server');
const sysDao = require('../dao/sys');

class ServerService {

    queryServerBySysId(req, res){
        const sysId = req.body.sysId;
        if(sysId === null || sysId===undefined){
            res.json({
                errMsg: "sysId不能为空",
                errCode: "sysId is required",
            });
            return;
        }
        serverDao.all(sysId).then(result => {
            res.json({
                list: result,
                errCode: "000000",
            });
        }, (err) => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        });
    }

    addServer(req, res){
        const requestMap = req.body;
        serverDao.insert(requestMap).then(result => {
            res.json({
                id: result,
                errCode: "000000",
            });
        }, (err) => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        });
    }

    updateServer(req, res){
        const id = req.body.id;
        if(id === null || id===undefined){
            res.json({
                errMsg: "id不能为空",
                errCode: "id is required",
            });
            return;
        }
        serverDao.update(req.body).then(result => {
            res.json({
                errCode: "000000"
            });
        }, (err) => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        });
    }

    deleteServer(req, res){
        const id = req.body.id;
        if(id === null || id===undefined){
            res.json({
                errMsg: "id不能为空",
                errCode: "id is required",
            });
            return;
        }
        serverDao.delete(id).then(result => {
            res.json({
                errCode: "000000"
            });
        },(err) => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        })
    }

}

const serverService = new ServerService();
module.exports = serverService;